﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Configuration;

namespace MyDb
{
    public class Db : IDisposable
    {
        SqlConnection myConn = null;
        string Connstring = "";
        public Db()
        {
            Connstring = System.Configuration.ConfigurationSettings.AppSettings["SqlConnectionString"];
            myConn = new SqlConnection(Connstring);
            myConn.Open();
        }
 
        public void Dispose() {
            myConn.Close();
            myConn.Dispose();
        }
        public SqlDataReader getList(string s_sql)
        {
            SqlConnection my_conn = new SqlConnection(Connstring);
            SqlCommand myComm = new SqlCommand(s_sql, my_conn);

            my_conn.Open();
            SqlDataReader res = myComm.ExecuteReader(CommandBehavior.CloseConnection);
            return res;
        }
        public object getScalar(string CmdText)
        {
            SqlConnection my_conn = new SqlConnection(Connstring);

            my_conn.Open();
            using (SqlCommand Cmd = new SqlCommand(CmdText, my_conn))
            {

                return Cmd.ExecuteScalar();
            }
        }
        public int getOne(string CmdText)
        {
            //SqlConnection my_conn = new SqlConnection(Connstring);
            int r = 0;
            //my_conn.Open();
            try
            {
                using (SqlCommand Cmd = new SqlCommand(CmdText, myConn))
                {

                    r = (int)Cmd.ExecuteScalar();
                }
            }
            catch { }
            //my_conn.Close();
            return r;
        }
        public Boolean Query(string CmdText)
        {
            SqlConnection my_conn = new SqlConnection(Connstring);
            int r = 0;
            my_conn.Open();
            using (SqlCommand Cmd = new SqlCommand(CmdText, my_conn))
            {

                r = (int)Cmd.ExecuteNonQuery();
            }
            my_conn.Close();
            return (r == 1);
        }
        public System.Data.DataSet getDataSet(string s_sql, string table_name)
        {
            #region 返回Adapter DataSet
            System.Data.DataSet ds = new System.Data.DataSet();
            using (SqlDataAdapter sql = new SqlDataAdapter(s_sql, myConn))
            {
                sql.Fill(ds, table_name);
            }
            return ds;
            #endregion
        }
        public System.Data.DataTable getDataTable(string s_sql, string table_name)
        {
            #region 返回DataTable
            System.Data.DataTable MyTable = new System.Data.DataTable(table_name);
            using (SqlDataAdapter sql = new SqlDataAdapter(s_sql, myConn))
            {
                sql.Fill(MyTable);
            }
            return MyTable;
            #endregion
        }
        public bool Cmd(string CmdText)
        {
            #region
            //SqlConnection my_conn = new SqlConnection(Connstring);

            //my_conn.Open();
            using (SqlCommand Cmd = new SqlCommand(CmdText, myConn))
            {
                return Cmd.ExecuteNonQuery() == 1;
            }
            //my_conn.Close();
            #endregion
        }

        public int MarkerShowNumber(string JoyID) {
            SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["SqlConnectionString"].ToString());
            int count = 0;
            connection.Open();
            using(SqlCommand cmd = new SqlCommand(string.Format("select joyvalue from joyconfig where joyid='{0}'",JoyID),connection)){
                SqlDataReader SDR = cmd.ExecuteReader();
                if (SDR.HasRows) {
                    while (SDR.Read()) {
                        count =Convert.ToInt16(SDR[0].ToString());
                    }
                }
            }
            connection.Close();
            return count;
        }
    }

    public static class JSON
    {
        public static T parse<T>(string jsonString)
        {
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
            {
                return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
            }
        }

        public static string stringify(object jsonObject)
        {
            using (var ms = new MemoryStream())
            {
                new DataContractJsonSerializer(jsonObject.GetType()).WriteObject(ms, jsonObject);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }
    }
}
